package com.fjnldx.mes.service.salaryReport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjnldx.mes.domain.domainReq.IdList;
import com.fjnldx.mes.domain.domainReq.QuerySalaryReport;
import com.fjnldx.mes.domain.domainReq.QuerySalaryReportTemplate;
import com.fjnldx.mes.domain.domainReq.UpdateSalaryReportTemplate;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.pojo.SalaryReportTemplateXs;
import com.fjnldx.mes.domain.pojo.SalaryReportXs;
import com.fjnldx.mes.domain.pojo.VarietyParameters;
import com.fjnldx.mes.domain.pojo.VarietyParametersReq;
import com.fjnldx.mes.mapper.salaryReport.SalaryReportMapper;
import com.fjnldx.mes.mapper.salaryReport.SalaryReportTemplateXsMapper;
import com.fjnldx.mes.mapper.salaryReport.SalaryReportXsMapper;
import com.fjnldx.mes.mapper.salaryReport.VarietyParametersMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Service
public class SalaryReportService {
    @Resource
    VarietyParametersMapper varietyParametersMapper;
    @Resource
    SalaryReportMapper salaryReportMapper;
    @Resource
    SalaryReportXsMapper salaryReportXsMapper;
    @Resource
    SalaryReportTemplateXsMapper salaryReportTemplateXsMapper;

    public Result updateTemplateFieldAttribution(UpdateSalaryReportTemplate data) {

        List<Integer> selectId = data.getSelectId();
        String gongxu = data.getGongxu();
        Integer templateNumber = data.getTemplateNumber();
        String columnName = "template" + templateNumber;
        switch (gongxu) {
            case "细纱":
                QueryWrapper<SalaryReportTemplateXs> wrapper = new QueryWrapper<>();
                wrapper.clear();
                wrapper.in("id", selectId);
                SalaryReportTemplateXs salaryReportTemplateXs = new SalaryReportTemplateXs();
                SalaryReportTemplateXs unSalaryReportTemplateXs = new SalaryReportTemplateXs();
                if ("template1".equals(columnName)) {
                    salaryReportTemplateXs.setTemplate1("1");
                    unSalaryReportTemplateXs.setTemplate1("0");
                } else if ("template2".equals(columnName)) {
                    salaryReportTemplateXs.setTemplate2("1");
                    unSalaryReportTemplateXs.setTemplate2("0");
                } else if ("template3".equals(columnName)) {
                    salaryReportTemplateXs.setTemplate3("1");
                    unSalaryReportTemplateXs.setTemplate3("0");
                } else if ("template4".equals(columnName)) {
                    salaryReportTemplateXs.setTemplate4("1");
                    unSalaryReportTemplateXs.setTemplate4("0");
                }
                salaryReportTemplateXsMapper.update(salaryReportTemplateXs, wrapper);
                wrapper.clear();
                wrapper.notIn("id", selectId);
                salaryReportTemplateXsMapper.update(unSalaryReportTemplateXs, wrapper);
                return Result.success("修改成功");
            default:
                return Result.error("暂无此工序");
        }
    }

    /**
     * 查询模板字段归属
     *
     * @param data
     * @return
     */
    public Result queryTemplateFieldAttribution(QuerySalaryReportTemplate data) {
        String gongxu = data.getGongxu();
        Integer templateNumber = data.getTemplateNumber();
        String columnName = "template" + templateNumber;
        switch (gongxu) {
            case "细纱":
                HashMap<String, Object> map = new HashMap<>();
                QueryWrapper<SalaryReportTemplateXs> wrapper = new QueryWrapper<>();
                wrapper.clear();
                wrapper.select("*", columnName + " flag");
                wrapper.eq("basicInfo", "1");
                List<SalaryReportTemplateXs> basicInfo = salaryReportTemplateXsMapper.selectList(wrapper);
                map.put("basicInfo", basicInfo);
                wrapper.clear();
                wrapper.select("*", columnName + " flag");
                wrapper.eq("varietiesParameterInfo", "1");
                List<SalaryReportTemplateXs> varietiesParameterInfo = salaryReportTemplateXsMapper.selectList(wrapper);
                map.put("varietiesParameterInfo", varietiesParameterInfo);
                wrapper.clear();
                wrapper.select("*", columnName + " flag");
                wrapper.eq("yieldInfo", "1");
                List<SalaryReportTemplateXs> yieldInfo = salaryReportTemplateXsMapper.selectList(wrapper);
                map.put("yieldInfo", yieldInfo);
                wrapper.clear();
                wrapper.select("*", columnName + " flag");
                wrapper.eq("salaryInfo", "1");
                List<SalaryReportTemplateXs> salaryInfo = salaryReportTemplateXsMapper.selectList(wrapper);
                map.put("salaryInfo", salaryInfo);
                return Result.success(map);
            default:
                return Result.error("暂无此工序");
        }
    }

    /**
     * 查询薪资报表
     */
    public Result querySalaryReport(QuerySalaryReport data) {
        int page = data.getPage();
        int size = data.getSize();
        LocalDate time = data.getTime();
        String gongxu = data.getGongxu();
        String bc = data.getBc();
        String pz = data.getPz();
        String cj = data.getCj();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String timeName = dateTimeFormatter.format(time);
        try {
            if ("细纱".equals(gongxu)) {
                Page<SalaryReportXs> iPage = new Page<>(page, size);
                QueryWrapper<SalaryReportXs> wrapper = new QueryWrapper<>();
                wrapper.apply(String.format("DATEDIFF(time,'%s')=0", time));
                String tableName = "history_xs_" + timeName;
                if (LocalDate.now().getDayOfMonth() == time.getDayOfMonth()) {
                    tableName = "xsmzl_table";
                }
                salaryReportXsMapper.delete(wrapper);
                salaryReportMapper.generateSalaryReportXs(time, tableName);
                if (StringUtils.isNotBlank(bc)) {
                    wrapper.eq("bc", bc);
                }
                if (StringUtils.isNotBlank(pz)) {
                    wrapper.eq("pz", pz);
                }
                if (StringUtils.isNotBlank(cj)) {
                    wrapper.eq("cj", cj);
                }
                wrapper.orderByAsc("workType","devSn");
                Page<SalaryReportXs> res = salaryReportXsMapper.selectPage(iPage, wrapper);
                return Result.success(res);
            }
            return Result.error("工序错误");
        }catch (Exception e){
            return Result.error("无"+time+"数据");
        }


    }

    /**
     * 品种公定参数-查询
     *
     * @param params
     * @return
     */
    public Result queryVarietyParameters(VarietyParametersReq params) {
        int page = params.getPage();
        int size = params.getSize();
        String cj = params.getCj();
        String pzlx = params.getPzlx();
        String dcpzxs = params.getDcpzxs();
        String lspzxs = params.getLspzxs();
        String dcdjxs = params.getDcdjxs();
        String lsdjxs = params.getLsdjxs();
        String pz = params.getPz();
        String zs = params.getZs();
        String dcbzkts = params.getDcbzkts();
        String lsbzkts = params.getLsbzkts();
        String pzgdl = params.getPzgdl();
        String cdtb12 = params.getCdtb12();
        String decl = params.getDecl();
        String sjlscs = params.getSjlscs();
        String dcdj = params.getDcdj();
        String dcjsgz8 = params.getDcjsgz8();
        String lsdj = params.getLsdj();
        String lsjsgz8 = params.getLsjsgz8();
        String lsqjdj = params.getLsqjdj();
        String lsdjCs = params.getLsdjCs();
        String hsdj = params.getHsdj();
        String bgdj = params.getBgdj();
        String txsdj = params.getTxsdj();
        String dcsfjj = params.getDcsfjj();
        String lssfjj = params.getLssfjj();
        String dcdjYfa = params.getDcdjYfa();
        String lsdjYfa = params.getLsdjYfa();
        String lsqjdjYfa = params.getLsqjdjYfa();
        String lsdjCsYfa = params.getLsdjCsYfa();
        String hsdjYfa = params.getHsdjYfa();
        String bgdjYfa = params.getBgdjYfa();
        String txsdjYfa = params.getTxsdjYfa();
        String ch = params.getCh();
        String ds = params.getDs();
        String jtxs = params.getJtxs();

        QueryWrapper<VarietyParameters> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(cj)) {
            wrapper.eq("cj", cj);
        }
        if (StringUtils.isNotBlank(pzlx)) {
            wrapper.eq("pzlx", pzlx);
        }
        if (StringUtils.isNotBlank(dcpzxs)) {
            wrapper.eq("dcpzxs", dcpzxs);
        }
        if (StringUtils.isNotBlank(lspzxs)) {
            wrapper.eq("lspzxs", lspzxs);
        }
        if (StringUtils.isNotBlank(dcdjxs)) {
            wrapper.eq("dcdjxs", dcdjxs);
        }
        if (StringUtils.isNotBlank(lsdjxs)) {
            wrapper.eq("lsdjxs", lsdjxs);
        }
        if (StringUtils.isNotBlank(pz)) {
            wrapper.eq("pz", pz);
        }
        if (StringUtils.isNotBlank(zs)) {
            wrapper.eq("zs", zs);
        }
        if (StringUtils.isNotBlank(dcbzkts)) {
            wrapper.eq("dcbzkts", dcbzkts);
        }
        if (StringUtils.isNotBlank(lsbzkts)) {
            wrapper.eq("lsbzkts", lsbzkts);
        }
        if (StringUtils.isNotBlank(pzgdl)) {
            wrapper.eq("pzgdl", pzgdl);
        }
        if (StringUtils.isNotBlank(cdtb12)) {
            wrapper.eq("cdtb12", cdtb12);
        }
        if (StringUtils.isNotBlank(decl)) {
            wrapper.eq("decl", decl);
        }
        if (StringUtils.isNotBlank(sjlscs)) {
            wrapper.eq("sjlscs", sjlscs);
        }
        if (StringUtils.isNotBlank(dcdj)) {
            wrapper.eq("dcdj", dcdj);
        }
        if (StringUtils.isNotBlank(dcjsgz8)) {
            wrapper.eq("dcjsgz8", dcjsgz8);
        }
        if (StringUtils.isNotBlank(lsdj)) {
            wrapper.eq("lsdj", lsdj);
        }
        if (StringUtils.isNotBlank(lsjsgz8)) {
            wrapper.eq("lsjsgz8", lsjsgz8);
        }
        if (StringUtils.isNotBlank(lsqjdj)) {
            wrapper.eq("lsqjdj", lsqjdj);
        }
        if (StringUtils.isNotBlank(lsdjCs)) {
            wrapper.eq("lsdjCs", lsdjCs);
        }
        if (StringUtils.isNotBlank(hsdj)) {
            wrapper.eq("hsdj", hsdj);
        }
        if (StringUtils.isNotBlank(bgdj)) {
            wrapper.eq("bgdj", bgdj);
        }
        if (StringUtils.isNotBlank(txsdj)) {
            wrapper.eq("txsdj", txsdj);
        }
        if (StringUtils.isNotBlank(dcsfjj)) {
            wrapper.eq("dcsfjj", dcsfjj);
        }
        if (StringUtils.isNotBlank(lssfjj)) {
            wrapper.eq("lssfjj", lssfjj);
        }
        if (StringUtils.isNotBlank(dcdjYfa)) {
            wrapper.eq("dcdjYfa", dcdjYfa);
        }
        if (StringUtils.isNotBlank(lsdjYfa)) {
            wrapper.eq("lsdjYfa", lsdjYfa);
        }
        if (StringUtils.isNotBlank(lsqjdjYfa)) {
            wrapper.eq("lsqjdjYfa", lsqjdjYfa);
        }
        if (StringUtils.isNotBlank(lsdjCsYfa)) {
            wrapper.eq("lsdjCsYfa", lsdjCsYfa);
        }
        if (StringUtils.isNotBlank(hsdjYfa)) {
            wrapper.eq("hsdjYfa", hsdjYfa);
        }
        if (StringUtils.isNotBlank(bgdjYfa)) {
            wrapper.eq("bgdjYfa", bgdjYfa);
        }
        if (StringUtils.isNotBlank(txsdjYfa)) {
            wrapper.eq("txsdjYfa", txsdjYfa);
        }
        if (StringUtils.isNotBlank(ch)) {
            wrapper.eq("ch", ch);
        }
        if (StringUtils.isNotBlank(ds)) {
            wrapper.eq("ds", ds);
        }
        if (StringUtils.isNotBlank(jtxs)) {
            wrapper.eq("jtxs", jtxs);
        }
        //wrapper.orderByDesc("id");
        IPage<VarietyParameters> iPage = new Page<>(page, size);
        IPage<VarietyParameters> varietyParametersIPage = varietyParametersMapper.selectPage(iPage, wrapper);
        return Result.success(varietyParametersIPage);
    }

    /**
     * 品种公定参数-新增
     *
     * @param params
     * @return
     */
    public Result addVarietyParameters(VarietyParameters params) {
        String pz = params.getPz();
        if (StringUtils.isBlank(pz)) {
            return Result.error("请输入品种");
        }
        QueryWrapper<VarietyParameters> wrapper = new QueryWrapper<>();
        wrapper.eq("pz", pz);
        Integer count = varietyParametersMapper.selectCount(wrapper);
        System.out.println(count);
        if (count != 0) {
            return Result.error("品种重复");
        }
        varietyParametersMapper.insert(params);
        return Result.success("新增成功");
    }

    /**
     * 品种公定参数-删除
     *
     * @param params
     * @return
     */
    public Result deleteVarietyParameters(IdList params) {
        List<Integer> idList = params.getIdList();
        varietyParametersMapper.deleteBatchIds(idList);
        return Result.success("删除成功");
    }

    /**
     * 品种公定参数-修改
     *
     * @param params
     * @return
     */
    public Result updateVarietyParameters(VarietyParameters params) {
        Integer id = params.getId();
        if (id == null) {
            return Result.error("id不能为空");
        }
        varietyParametersMapper.updateById(params);
        return Result.success("修改成功");
    }
}
